trigger AccountShare on Purchase_Object__c (after insert, after update) {
   
    list<AccountShare> ASlist = new list<AccountShare>();
    list<Purchase_Object__Share> POSlist = new list<Purchase_Object__Share>();
    list<Purchase_Object__Share> POSlist1 = [select id,parentid,USERORGROUPID from Purchase_Object__Share];
    list<Purchase_Object__Share> POSlistdel = new list<Purchase_Object__Share>();
    list<Account> acclist = new list<Account>();
    list<Account> ATlist = new list<Account>();
    ASlist = [select ID,ACCOUNTID,USERORGROUPID,ACCOUNTACCESSLEVEL from AccountShare  ];
    if(trigger.isupdate)
    {
        for(Purchase_Object__c po : trigger.new)
        {
            for(Purchase_Object__Share poshare :POSlist1)
            {
                if(po.id == poshare.parentid && po.Ownerid!=poshare.USERORGROUPID)
                {
                    POSlistdel.add(poshare);
                }
            }
        }
    }
    delete POSlistdel; 
    for(Purchase_Object__c po : trigger.new)
    {
        for(AccountShare as1: ASlist)
        {
            if(po.Account__c == as1.ACCOUNTID && as1.USERORGROUPID!=po.Ownerid)
            {
                Purchase_Object__Share pos = new Purchase_Object__Share();
                pos.ACCESSLEVEL= 'read' ;
                pos.USERORGROUPID= as1.USERORGROUPID;
                pos.PARENTID= po.id;
                POSlist.add(pos);
                system.debug('User ID'+as1.USERORGROUPID);
                system.debug('Parentid'+po.id);
            }
        }
    }         
    insert POSlist;   
           
}